package com.own.demo.长沙空中路网格子;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.own.demo.utils.GeoTools;
import com.own.demo.utils.geo.PoiTypeEnums;
import com.own.demo.utils.geo.buffer.LineBufferImpl;
import com.own.demo.utils.geo.buffer.PolygonBufferImpl;
import com.own.demo.vo.Bounds;
import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.Geometry;

import java.util.ArrayList;
import java.util.List;

public class Common {

    public static LineBufferImpl defaultBuffer = (LineBufferImpl) PoiTypeEnums.POLYLINE.getBufferUtil();
    public  static PolygonBufferImpl polygonBuffer = new PolygonBufferImpl();

    public static JSONObject dataDraw(List<List<Coordinate>> list, boolean isDelArea,List<Geometry> listBuildInfo) {
        JSONObject jsonObject = new JSONObject();
        JSONArray jsonArray = new JSONArray(1024);
        jsonObject.put("type", "GeometryCollection");
        int row = list.size();
        String geoType="LineString";
        for (int i = 0; i < row; i++) {
            jsonArray.addAll(setXYGeo(i, 0, list.get(i), geoType));
        }

        if(isDelArea) {
            //拿出数据，去掉空洞部分
            int dataIndexSize = listBuildInfo.size();
            for (int i = 0; i < dataIndexSize; i++) {
                jsonArray = dataInfo(jsonArray, listBuildInfo.get(i), geoType);
            }
        }
        jsonObject.put("features", jsonArray);
        return jsonObject;
    }



    public static JSONObject defaultObjectGsoJSON() {
        JSONObject jsonObject = new JSONObject();
        JSONArray jsonArray = new JSONArray(11024);
        jsonObject.put("type", "GeometryCollection");
        jsonObject.put("features", jsonArray);
        return jsonObject;
    }

    public static JSONObject defaultObjectGsoJSONName(String name) {
        JSONObject jsonObject =  defaultObjectGsoJSON();
        jsonObject.put("name",name);
        return jsonObject;
    }
    /**
     * 去除空洞部分
     * @param updateInfo updateInfo 全局数据
     * @param geometry   空洞边界
     * @return 空洞后的线
     */
    public static JSONArray dataInfo(JSONArray updateInfo, Geometry geometry, String geoType) {
        JSONArray jsonArray = new JSONArray(1024);
        int indexSize=updateInfo.size();
        for(int i=0;i<indexSize;i++){
            JSONArray points=updateInfo.getJSONObject(i).getJSONObject("geometry").getJSONArray("coordinates");
            int coordRow=points.size();
            StringBuffer stringBuffer = new StringBuffer();
            JSONArray gpsListAll = new JSONArray(coordRow);
            for (int h = 0; h < coordRow; h++) {
                JSONArray coordinate = points.getJSONArray(h);
                double[] gps = new double[2];
                gps[0] = coordinate.getDouble(0);
                gps[1] = coordinate.getDouble(1);
                stringBuffer.append(gps[0] + "," + gps[1] + ";");
                gpsListAll.add(gps);
            }
            if(stringBuffer.length() ==0){
                continue;
            }
            String dataInfo = stringBuffer.substring(0, stringBuffer.length() - 1);

            Geometry line = defaultBuffer.geometrySplit(dataInfo);
            Geometry geometryIn = line.intersection(geometry);
            if (!geometryIn.isEmpty()) {
                JSONArray gpsList = new JSONArray(coordRow);
                Coordinate[] coordinates1 = geometryIn.getBoundary().getCoordinates();
                for (int h = 0; h < coordRow; h++) {
                    JSONArray array = points.getJSONArray(h);
                    boolean flag = GeoTools.onLineSegment(new Coordinate(array.getDouble(0),array.getDouble(1)), coordinates1[0], coordinates1[1]);
                    if (flag) {
                        JSONArray gps = gpsList.getJSONArray(gpsList.size() - 1);
                        double distance1 = GeoTools.distance(new Coordinate(gps.getDouble(0), gps.getDouble(1)), coordinates1[0]);
                        double distance2 = GeoTools.distance(new Coordinate(gps.getDouble(0), gps.getDouble(1)), coordinates1[1]);

                        double[] gps1 = new double[2];
                        gps1[0] = coordinates1[0].x;
                        gps1[1] = coordinates1[0].y;

                        double[] gps2 = new double[2];
                        gps2[0] = coordinates1[1].x;
                        gps2[1] = coordinates1[1].y;

                        gpsList.add(distance1 <= distance2 ? gps1 : gps2);
                        addLingString(jsonArray, i, 0, geoType, gpsList);
                        addLingString(jsonArray, i, 1 + 1, geoType, addOther(h, points, coordRow, !(distance1 <= distance2) ? gps1 : gps2, coordinates1));
                        break;
                    }
                    gpsList.add(array);
                }
            }else{
                addLingString(jsonArray, i, 0, geoType, gpsListAll);
            }
        }
        return jsonArray;
    }

    public static JSONArray addOther(int h, JSONArray array , int coordRow, double[] statGps, Coordinate[] coordinates1) {
        JSONArray gpsList = new JSONArray(1024);
        gpsList.add(statGps);
        for (int x = h + 1; x < coordRow; x++) {
            JSONArray jsonArray = array.getJSONArray(x);
            boolean flag = GeoTools.onLineSegment(new Coordinate(jsonArray.getDouble(0),jsonArray.getDouble(1)), coordinates1[0], coordinates1[1]);
            if (!flag) {
                gpsList.add(jsonArray);
            }
        }
        return gpsList;
    }

    public static void addLingString(JSONArray jsonArray, int i, int j, String geoType, JSONArray gpsList) {
        JSONObject geo = new JSONObject(4);
        geo.put("type", "Feature");
        JSONObject geoPro = new JSONObject(1);
        geoPro.put("name", i > 0 ? j + "-" + i : "area");
        geo.put("properties", geoPro);
        JSONObject objectInfo = new JSONObject();
        objectInfo.put("coordinates", gpsList);
        objectInfo.put("type", geoType);
        geo.put("geometry", objectInfo);
        jsonArray.add(geo);
    }

    public static JSONArray setXYGeo(int i, int j, List<Coordinate> list, String geoType) {
        JSONArray jsonArray = new JSONArray(1024);
        Coordinate[] coordinates = list.toArray(new Coordinate[0]);
        int coordRow = coordinates.length;
        JSONArray gpsList = new JSONArray(coordRow);
        for (int h = 0; h < coordRow; h++) {
            Coordinate coordinate = coordinates[h];
            double[] gps = new double[2];
            gps[0] = coordinate.x;
            gps[1] = coordinate.y;
            gpsList.add(gps);
        }
        addLingString(jsonArray, i, j, geoType, gpsList);
        return jsonArray;
    }
}
